/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestBarcodeFrame.java
 *
 * Created on Nov 26, 2011, 8:31:45 PM
 */
package eyeofthetiger.gui;

import eyeofthetiger.model.Participant;
import eyeofthetiger.model.Project;
import eyeofthetiger.utils.Utils;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.observablecollections.ObservableList;

/**
 *
 * @author christophe
 */
public class TestBarcodeFrame extends javax.swing.JFrame {

    protected static final ResourceBundle MSGS = ResourceBundle.getBundle(MBAction.classBundleBaseName(TestBarcodeFrame.class));
    
    
    private Project project;
    
    /** Creates new form TestBarcodeFrame */
    public TestBarcodeFrame(Project p) {
        if(p!=null) {
            project = p;
            participants.addAll(project.getParticipants());
        }
        
        initComponents();

        jPanelTest1.setBackground(Color.WHITE);
        jPanelTest1.setOpaque(true);
        jPanelTest2.setBackground(Color.WHITE);
        jPanelTest2.setOpaque(true);
        jPanelTest1Inner.setBackground(Color.WHITE);
        jPanelTest1Inner.setOpaque(true);
        jPanelTest2Inner.setBackground(Color.WHITE);
        jPanelTest2Inner.setOpaque(true);
        
        jPanelTest1.removeAll();
        jPanelTest1.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanelTest1.add(jPanelTest1Inner);
        jPanelTest1Inner.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanelTest1Inner.add(getTest1());
        
        jPanelTest2.removeAll();
        jPanelTest2.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanelTest2.add(jPanelTest2Inner);
        jPanelTest2Inner.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanelTest2Inner.add(getTest2());
        
        jTextFieldCode.setFont(Utils.BigFont);
        eyeofthetiger.gui.Utils.BalloonHelp("<html><p width='200px'>Donnez le focus pour faire fonctionner le lecteur de code barre.<br/> Le champs devient alors violet.</p></html>", jTextFieldCode);
        
        TableRowSorter<TableModel> sorterCourseParticipants = new TableRowSorter<TableModel>(jTableParticipant.getModel());
        jTableParticipant.setRowSorter(sorterCourseParticipants);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableParticipant = new javax.swing.JTable();
        jPanelParticipant = new javax.swing.JPanel();
        jPanelParticipantPanel = new javax.swing.JPanel();
        jPanelTest1 = new javax.swing.JPanel();
        jPanelTest1Inner = new javax.swing.JPanel();
        jPanelTest2 = new javax.swing.JPanel();
        jPanelTest2Inner = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldCode = new javax.swing.JTextField();
        jPanelResultat = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableParticipant.setName("jTableParticipant"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${participants}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTableParticipant);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${numero}"));
        columnBinding.setColumnName("Numero");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nom}"));
        columnBinding.setColumnName("Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prenom}"));
        columnBinding.setColumnName("Prenom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${groupe}"));
        columnBinding.setColumnName("Groupe");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${renseignements}"));
        columnBinding.setColumnName("Renseignements");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${selectedParticipant}"), jTableParticipant, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(jTableParticipant);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/resources/TestBarcodeFrame"); // NOI18N
        jTableParticipant.getColumnModel().getColumn(0).setHeaderValue(bundle.getString("TestBarcodeFrame.jTableParticipant.columnModel.title0")); // NOI18N
        jTableParticipant.getColumnModel().getColumn(1).setHeaderValue(bundle.getString("TestBarcodeFrame.jTableParticipant.columnModel.title1")); // NOI18N
        jTableParticipant.getColumnModel().getColumn(2).setHeaderValue(bundle.getString("TestBarcodeFrame.jTableParticipant.columnModel.title2")); // NOI18N
        jTableParticipant.getColumnModel().getColumn(3).setHeaderValue(bundle.getString("TestBarcodeFrame.jTableParticipant.columnModel.title3")); // NOI18N
        jTableParticipant.getColumnModel().getColumn(4).setHeaderValue(bundle.getString("TestBarcodeFrame.jTableParticipant.columnModel.title4")); // NOI18N

        jPanelParticipant.setBackground(new java.awt.Color(255, 255, 255));
        jPanelParticipant.setForeground(new java.awt.Color(255, 255, 255));
        jPanelParticipant.setName("jPanelParticipant"); // NOI18N

        jPanelParticipantPanel.setName(bundle.getString("TestBarcodeFrame.jPanelParticipantPanel.name")); // NOI18N

        javax.swing.GroupLayout jPanelParticipantPanelLayout = new javax.swing.GroupLayout(jPanelParticipantPanel);
        jPanelParticipantPanel.setLayout(jPanelParticipantPanelLayout);
        jPanelParticipantPanelLayout.setHorizontalGroup(
            jPanelParticipantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPanelParticipantPanelLayout.setVerticalGroup(
            jPanelParticipantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jPanelParticipant.add(jPanelParticipantPanel);

        jPanelTest1.setBorder(javax.swing.BorderFactory.createTitledBorder("Test 1"));
        jPanelTest1.setName("jPanelTest1"); // NOI18N

        jPanelTest1Inner.setName("jPanelTest1Inner"); // NOI18N

        javax.swing.GroupLayout jPanelTest1InnerLayout = new javax.swing.GroupLayout(jPanelTest1Inner);
        jPanelTest1Inner.setLayout(jPanelTest1InnerLayout);
        jPanelTest1InnerLayout.setHorizontalGroup(
            jPanelTest1InnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanelTest1InnerLayout.setVerticalGroup(
            jPanelTest1InnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 74, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanelTest1Layout = new javax.swing.GroupLayout(jPanelTest1);
        jPanelTest1.setLayout(jPanelTest1Layout);
        jPanelTest1Layout.setHorizontalGroup(
            jPanelTest1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTest1Inner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanelTest1Layout.setVerticalGroup(
            jPanelTest1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTest1Inner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanelTest2.setBorder(javax.swing.BorderFactory.createTitledBorder("Test 2"));
        jPanelTest2.setName("jPanelTest2"); // NOI18N

        jPanelTest2Inner.setName("jPanelTest2Inner"); // NOI18N

        javax.swing.GroupLayout jPanelTest2InnerLayout = new javax.swing.GroupLayout(jPanelTest2Inner);
        jPanelTest2Inner.setLayout(jPanelTest2InnerLayout);
        jPanelTest2InnerLayout.setHorizontalGroup(
            jPanelTest2InnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanelTest2InnerLayout.setVerticalGroup(
            jPanelTest2InnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 104, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanelTest2Layout = new javax.swing.GroupLayout(jPanelTest2);
        jPanelTest2.setLayout(jPanelTest2Layout);
        jPanelTest2Layout.setHorizontalGroup(
            jPanelTest2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTest2Inner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanelTest2Layout.setVerticalGroup(
            jPanelTest2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTest2Inner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lecture:"));
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel1.setText(MSGS.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextFieldCode.setFont(new java.awt.Font("Dialog.plain", 0, 12)); // NOI18N
        jTextFieldCode.setText(MSGS.getString("jTextFieldCode.text")); // NOI18N
        jTextFieldCode.setName("jTextFieldCode"); // NOI18N
        jTextFieldCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCodeActionPerformed(evt);
            }
        });
        jTextFieldCode.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldCodeFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldCodeFocusLost(evt);
            }
        });

        jPanelResultat.setName("jPanelResultat"); // NOI18N

        javax.swing.GroupLayout jPanelResultatLayout = new javax.swing.GroupLayout(jPanelResultat);
        jPanelResultat.setLayout(jPanelResultatLayout);
        jPanelResultatLayout.setHorizontalGroup(
            jPanelResultatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanelResultatLayout.setVerticalGroup(
            jPanelResultatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldCode, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
            .addComponent(jPanelResultat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelResultat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTest1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanelTest2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanelParticipant, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelTest1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelTest2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelParticipant, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents


private void jTextFieldCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCodeActionPerformed
    String code = jTextFieldCode.getText().trim();
    if(code.isEmpty()) {
        return;
    }
    
    boolean found = false;
    if(code.equals(CodeTest1)) {
        resultat("Test 1 !");
        found = true;
    }
    else if(code.equals(CodeTest2)) {
        resultat("Test 2 !");
        found = true;
    }
    else 
    {
        for(Participant p : participants) {
            if(code.equals(p.getNumero())) {
                resultat(p);
                found = true;
                break;
            }
        }
    }
    
    if(found) {
        jTextFieldCode.setText("");
        Utils.OkSound();
    }
    else {
        resultat("Inconnu !");
        Utils.BadSound();
    }
}//GEN-LAST:event_jTextFieldCodeActionPerformed

private void jTextFieldCodeFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldCodeFocusGained
    jTextFieldCode.setBackground(Utils.LightBlue);
}//GEN-LAST:event_jTextFieldCodeFocusGained

private void jTextFieldCodeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldCodeFocusLost
    jTextFieldCode.setBackground(Color.white);
}//GEN-LAST:event_jTextFieldCodeFocusLost


    private void resultat(Object o) {
        jPanelResultat.removeAll();
        //jPanelResultat.setBackground(null);
        jPanelResultat.setLayout(new BorderLayout());
        if(o == null) {
            //Not found !   
            jPanelResultat.setBackground(Color.red.brighter().brighter());
            JLabel label = new JLabel("Code non trouvé !");
            jPanelResultat.add(label,BorderLayout.CENTER);
        }
        else if(o instanceof String) {
            JLabel label = new JLabel((String)o);
            label.setFont(Utils.BigFont);
            jPanelResultat.setLayout(new FlowLayout(FlowLayout.CENTER));
            jPanelResultat.add(label);
        }
        else if(o instanceof Participant) {
            //ParticipantPanel p = new ParticipantPanel((Participant)o);
            (new ParticipantPanel.CreateParticipantPanelWorker((Participant)o) {
                @Override
                protected void use(ParticipantPanel pp) {
                    jPanelResultat.setLayout(new FlowLayout(FlowLayout.CENTER));
                    jPanelResultat.add(pp);
                    jPanelResultat.updateUI();
                }
            }).execute();
            
            //p.setBarcodeVisible(true);
            //jPanelResultat.add(p,BorderLayout.CENTER);
        }
        jPanelResultat.updateUI();
    }

    public static String CodeTest1 ="a123456789";
    public static String CodeTest2 ="abcdef123";

    
    protected ObservableList<Participant> participants = ObservableCollections.observableList(new ArrayList<Participant>());
    public ObservableList<Participant> getParticipants() {
        return participants;
    }    
    
    protected Participant selectedParticipant;
    public void setSelectedParticipant(Participant p) {
        selectedParticipant = p;
        if(selectedParticipant != null) {
            (new ParticipantPanel.CreateParticipantPanelWorker(selectedParticipant) {
                @Override
                protected void use(ParticipantPanel pp) {
                    pp.setOpaque(true);
                    pp.setBackground(Color.WHITE);
                    jPanelParticipant.setBackground(Color.WHITE);
                    jPanelParticipant.setOpaque(true);
                    jPanelParticipant.removeAll();
                    jPanelParticipant.setLayout(new FlowLayout(FlowLayout.CENTER));
                    pp.setPreferredSize(null);
                    jPanelParticipant.add(pp);
                    jPanelParticipant.updateUI();
                    
                }
            }).execute();
        }
    }
    public Participant getSelectedParticipant() {
        return selectedParticipant;
    }
    
    private JComponent getTest1() {
        return Utils.BarcodeFor(CodeTest1);
    }

    private JComponent getTest2() {
        return Utils.BarcodeFor(CodeTest2);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelParticipant;
    private javax.swing.JPanel jPanelParticipantPanel;
    private javax.swing.JPanel jPanelResultat;
    private javax.swing.JPanel jPanelTest1;
    private javax.swing.JPanel jPanelTest1Inner;
    private javax.swing.JPanel jPanelTest2;
    private javax.swing.JPanel jPanelTest2Inner;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableParticipant;
    private javax.swing.JTextField jTextFieldCode;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
